<template>
  <Dialog title="批量上传" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="声纹" prop="name">
        <el-select style="width:200px;" v-model="formData.audioToneId" placeholder="请选择声纹">
          <el-option :value="item.id" :label="item.name" :key="item.id" v-for="item in audioToneList" />
        </el-select >
      </el-form-item>
      <el-form-item label="标签" prop="tagIdArray">
        <el-select filterable multiple v-model="formData.tagIdArray" placeholder="请输入标签id数组，一个音频可以有多个标签">
          <el-option v-for="item in tagList" :key="item.id" :value="item.id" :label="item.tagName" />
        </el-select>
      </el-form-item>
      <el-form-item label="音频文件" prop="fileId">
        <CustomUploadFile :multiple="true" :fileTypeList="['mp3', 'aac']" :show-play-btn="true" :default-list="defaultFileList"  @upload-finish="uploadFinish" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { AudioApi, AudioVO } from '@/api/lm/audio'
import CustomUploadFile from '@/components/CustomUploadFile/index.vue';

/** 音频 表单 */
defineOptions({ name: 'AudioBatchForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  fileList: [],
  tagIdArray: [],
  audioToneId: ""
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref
const tagList = ref([]);
const audioToneList = ref([]);

/** 打开弹窗 */
const open = async (options:any = {}) => {
  resetForm()
  dialogVisible.value = true
  tagList.value = options.tagList;
  audioToneList.value = options.audioToneList;
  formData.value.audioToneId = options.audioToneId;
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as AudioVO
    await AudioApi.createBatch(data)
    message.success(t('common.createSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

const uploadFinish = (e:any)=> {
  let successList = e.successList;

  if(!successList) {
    return message.warning("没有文件上传成功，请检查");
  }

  console.log(e);
  formData.value.fileList = successList.map(item=> {
    let fileName = item.file.name.substring(0, item.file.name.lastIndexOf("."));
    return {
      fileUrl: item.fileUrl,
      fileName,
      path: item.path,
      fileId: item.fileId,
      audioSize: successList[0].audioDuration
    }
  })
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    fileList: [],
    tagIdArray: [],
    audioToneId: ""
  }
  formRef.value?.resetFields()
}
</script>
